package five;
import java.util.*;
public class A086 {


    //139.单词拆分
    public boolean wordBreak(String s, List<String> wordDict) {
        //1.存储字典集
        Set<String> set = new HashSet<>();
        for(String t:wordDict) {
            set.add(t);
        }
        int n = s.length();
        boolean[] dp = new boolean[n+1];//dp[i]:0~i这段字符是否找到
        s = " "+s;
        dp[0]=true;
        for(int i=1;i<n+1;i++) {
            for(int j=i;j>=1;j--) {
                if(dp[j-1] && set.contains(s.substring(j,i+1))) { //dp[i]=dp[0-j] && dp[j-i]
                    dp[i]=true;
                    break;
                }
            }
        }
        return dp[n];
    }
}
